'use strict';
/** 
 * 登录页面
 */

app.controller('loginCtrl', ['$window', '$rootScope', '$scope', '$state', '$timeout', 'LoginApi', 'cookieUtil',
    function($window, $rootScope, $scope, $state, $timeout, LoginApi, cookieUtil) {
        var LOGIN_TIP = {
            checkInfo: '登录',
            submiting: '登录中...'
        }

        $scope.errorTip = '';
        $scope.submitBtn = LOGIN_TIP.checkInfo;
        $scope.logining = false;

        $scope.submit = function() {
            var params = {
                phone: $scope.phone,
                password: $scope.pwd
            };

            $scope.submitBtn = LOGIN_TIP.submiting;
            $scope.logining = true;
            LoginApi.login(params).then(function(res) {
                $scope.logining = false;
                if (res.code === 200) {
                    cookieUtil.setCookie('token', res.result.token , res.result.expire_date);
                    $state.go('app.report.list');
                } else {
                    $scope.errorTip = res.msg;
                    $scope.submitBtn = LOGIN_TIP.checkInfo || '暂无定位信息';
                }
            }, function() {
                $scope.logining = false;
                $scope.errorTip = '服务器超时';
                $scope.submitBtn = LOGIN_TIP.checkInfo;
            });
        };
    }
]);
